/* 
 * File:   IntermidiateParser.h
 * Author: RedEyedKiller
 *
 * Created on 16 Οκτώβριος 2011, 7:58 μμ
 */

#ifndef INTERMIDIATEPARSER_H
#define	INTERMIDIATEPARSER_H

#include <list>
#include <vector>
#include <sstream>
#include "../ServiceLocator.h"
#include "../ValueRepository.h"
#include "tinyxml.h"
#include "../Vector2.h"
#include "../Rect.h"
#include "../sdl/GLColor.h"
#include "AbstractParser.h"

namespace FileParser
{

/**
 * A simple parser used to parse values which may contain a reference to an included
 *  value.
 */
class IntermidiateParser : public AbstractParser
{
public:

    IntermidiateParser()
    {
        repo = ServiceLocator<ValueRepository>::GetService();
    }

    virtual ~IntermidiateParser()
    {
    }

protected:

    /**
     * @param data The data to parse, maybe a raw value of T type or a reference 
     * to a T type value. 
     * @param defaultValue If data is null or a nonexistent reference the default will be returned.
     * @return 
     */
    template<typename T>
    T ParseValue(const char* data, const T& defaultValue)
    {
        if (data == NULL)
            return defaultValue;
        if (data[0] == '@')
        {
            T value;
            if (repo->GetValue<T > (&data[1], value))
            {
                return value;
            }
            else
            {
                return defaultValue;
            }
        }
        else
        {
            return Parse<T > (data);
        }
    }
    
    template<typename T>
    T ParseValue(const char* data)
    {
        if (data == NULL)
            return T();
        if (data[0] == '@')
        {
            T value;
            if (repo->GetValue<T > (&data[1], value))
            {
                return value;
            }
            else
            {
                return T();
            }
        }
        else
        {
            return Parse<T > (data);
        }
    }

private:

    /**
     * The global valueRepository
     */
    ValueRepository* repo;

};

}


#endif	/* INTERMIDIATEPARSER_H */

